#include "internal.h"
#include "Device.tmh"
//#include "ntddser.h"

#include "initguid.h"

Device::Device(VOID)
    : m_FxDevice(NULL)
{
}

HRESULT Device::Initialize(__in IWDFDriver *FxDriver, __in IWDFDeviceInitialize *FxDeviceInit) {

//    HRESULT hr;
//    hr = m_FxDevice->CreateDeviceInterface(&GUID_DEVINTERFACE_COMPORT, NULL);
//
//    if (S_OK == hr)
//        hr = m_FxDevice->AssignDeviceInterfaceState(&GUID_DEVINTERFACE_COMPORT, NULL, TRUE);
//
//    return hr;

    return S_OK;
}

HRESULT Device::CreateInstance(__in IWDFDriver *FxDriver, __in IWDFDeviceInitialize *FxDeviceInit, __out Device **resultDevice) {
    Device *device;
    HRESULT hr;

    device = new Device();

    if (NULL == device)
        return E_OUTOFMEMORY;

    hr = device->Initialize(FxDriver, FxDeviceInit);

    if (SUCCEEDED(hr))
        *resultDevice = device;
    else 
        device->Release();

    return hr;
}

HRESULT Device::Configure(VOID) {
    return S_OK;
}

ULONG STDMETHODCALLTYPE Device::AddRef(VOID) {
    return __super::AddRef();
}

ULONG STDMETHODCALLTYPE Device::Release(VOID) {
    return __super::Release();
}

HRESULT STDMETHODCALLTYPE Device::QueryInterface(__in REFIID InterfaceId, __out PVOID *Interface) {
    return CUnknown::QueryInterface(InterfaceId, Interface);
}

STDMETHODIMP_ (void) Device::OnCreateFile(IWDFIoQueue* pQueue, IWDFIoRequest* pRequest, IWDFFile* pFileObject)
{
    (pQueue);
    (pFileObject);
    pRequest->Complete(S_OK);
    return;
}

STDMETHODIMP_ (void) Device::OnCloseFile(IWDFFile* pFileObject)
{
    (pFileObject);

    return;
}


STDMETHODIMP_ (void) Device::OnCleanupFile(IWDFFile* pFileObject)
{
    (pFileObject);

    return;
}

STDMETHODIMP_ (void) Device::OnRead(IWDFIoQueue* pQueue, IWDFIoRequest* pRequest, SIZE_T NumOfBytesToRead)
{
    (pQueue);
    (pRequest);
    (NumOfBytesToRead);

    IWDFMemory *memory;
    pRequest->GetOutputMemory(&memory);
    SIZE_T size;
    char *buffer = (char*)memory->GetDataBuffer(&size);
    memset(buffer, 0, size);
    pRequest->CompleteWithInformation(S_OK, 0);
}

STDMETHODIMP_ (void) Device::OnWrite(IWDFIoQueue* pQueue, IWDFIoRequest* pRequest, SIZE_T NumOfBytesToWrite)
{
    (pQueue);
    (pRequest);
    (NumOfBytesToWrite);

    HRESULT hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
    pRequest->CompleteWithInformation(hr, 0);
}


STDMETHODIMP_ (void) Device::OnDeviceIoControl(IWDFIoQueue* pQueue, IWDFIoRequest* pRequest, ULONG ControlCode, SIZE_T InputBufferSizeInBytes, SIZE_T OutputBufferSizeInBytes)
{
    HRESULT hr          = E_UNEXPECTED;
    DWORD   dwWritten   = 0;

    hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
    pRequest->CompleteWithInformation(hr, dwWritten);
}
